<template>
	<view>
		<view class="charts">
			<view class="title">
				历史成绩
			</view>
			<view class="title2">
				平均分：{{average.toFixed(2)}}
			</view>
			<view class="charts-box">
				<qiun-data-charts type="line" :chartData="chartData" :errorShow="false" background="none"/>
			</view>
		</view>
		<u-gap height="20" bg-color="#FAFAFA"></u-gap>
		<view class="charts">
			<view class="title">
				季考历史详情
			</view>
			<view class="test" v-if="jkhistoryList.length > 0">
				<u-cell-item v-for="(item,index) in jkhistoryList" :key="index" class="test1" @click="historyClick(item)" :title="(item.beginTime ? item.beginTime.slice(0,10) : '') + '      成绩：'+ (item.score ? item.score : 0)"></u-cell-item>
			</view>
		</view>
		<view class="charts">
			<view class="title">
				考试历史详情
			</view>
			<view class="test" v-if="historyList.length > 0">
				<u-cell-item v-for="(item,index) in historyList" :key="index" class="test1" @click="historyClick(item)" :title="(item.beginTime ? item.beginTime.slice(0,10) : '') + '      成绩：'+ (item.score ? item.score : 0)"></u-cell-item>
			</view>
		</view>
		<u-gap height="20" bg-color="#FAFAFA"></u-gap>
		<view class="charts">
			<view class="title">
				待考试
			</view>
			<view class="test" v-if="testList.length > 0">
				<u-cell-item v-for="(item,index) in testList" :key="index" class="test1" :title="item.name" value="去考试" @click="testClick(item)"></u-cell-item>
			</view>
		</view>
	</view>
</template>

<script>
	import {examsUnfinishedExam,examsStatistics,usableExams,examsHistory} from '../../api/api.js'
	export default {
		data() {
			return {
				customStyleButton:{
					marginTop:'80rpx',
					backgroundColor:'#F18B3A',
					borderRadius: '12rpx',
					height: '88rpx'
				},
				testObj:{},
				chartData:{
					"categories": [],
					"series": [
						{
							"name":'成绩',
							"lineType": "solid",
							"color": "#F18B3A",
							"textColor": "#F18B3A",
							"data": []
						},
					]
				},
				testList:[],	// 考试列表
				historyList:[],	// 历史记录
				jkhistoryList: [],
				average:0
			}
		},
		onShow() {
			this.getUsableExams()
			this.getExamsHistory()
		},
		mounted() {
			
		},
		methods: {
			// 历史详情
			historyClick(item){
				uni.navigateTo({
					url: `../results/results?id=${item.id}`
				})
			},
			getExamsHistory(){
				this.jkhistoryList = []
				this.historyList = []
				let parmes = {
					beginDate: '2021-06-01'
				}
				examsHistory(parmes).then(resp=>{
					let xAis = [],data = [],num = 0
					resp.forEach(e=>{
						if(e.endTime && e.score){
							xAis.push(e.endTime.slice(0,10))
							data.push(e.score)
							num += e.score
						}
						
					})
					this.average = num/resp.length || 0
					this.chartData.categories = xAis
					this.chartData.series[0].data = data
					resp.forEach(item=>{
						if(item.type && item.type == 2){
							this.jkhistoryList.push(item)
						}else{
							this.historyList.push(item)
						}
					})
				})
			},
			// 用户考试列表
			getUsableExams(){
				usableExams().then(resp=>{
					this.testList = resp
				})
			},
			testClick(item){
				uni.navigateTo({
					url: `../choose/choose?examPlanId=${item.id}`
				})
			},
			getExamsStatistics(){
				examsStatistics().then(resp=>{
					this.testObj = resp
				})
			},
			// 获取本人未结束的考试
			getExams(){
				examsUnfinishedExam().then(resp=>{
					
					if(resp== ''){
						uni.navigateTo({
							url: '../choose/choose'
						})
					}else{
						uni.navigateTo({
							url: '../choose/choose?flag=true'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
.charts{
	padding: 20rpx;
	.title{
		font-size: 36rpx;
		height: 80rpx;
		line-height: 80rpx;
		font-weight: bold;
	}
	.title2{
		font-size: 26rpx;
		color: #6C7B8A;
		line-height: 30rpx;
		height: 30rpx;
	}
	.charts-box{
		width: 100%;
		height:200px;
	}
}
</style>
